<?php
namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\data\Pagination;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\helpers\Json;
use backend\models\DepartmentForm;
use common\models\Department;
use yii\helpers\ArrayHelper;
use common\models\EmpDepart;
use yii\web\ForbiddenHttpException;

/**
 * Department controller
 */
class DepartmentController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['create', 'datalist', 'distribute', 'departs'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    /*
    * 新建部门
     */
    public function actionCreate($id=null)
    {
        if(!Yii::$app->user->can('createDepartment')){
            throw new ForbiddenHttpException('对不起，您现在还没获得此操作的权限');
        }

        $model = new DepartmentForm($id);
        $de_arr = Department::find()->where(['status' => 1])->asArray()->all();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if ($model->addOrUpdate()) {

                return $this->redirect(['department/datalist']);
            }
        }
        if (!empty($id)) {
            $employee = EmpDepart::find()->joinWith('employee')->where(['emp_depart.did' => $id, 'emp_depart.identity' => EmpDepart::EMPLOYEE])->asArray()->all();
            $manager = EmpDepart::find()->joinWith('employee')->where(['emp_depart.did' => $id, 'emp_depart.identity' => EmpDepart::MANAGER])->asArray()->all();
            $manager_arr = [];
            $employee_arr = [];
            foreach ($manager as $val) {
                $re = ArrayHelper::merge($val, $val['employee']);
                array_push($manager_arr, $re);
            }
            $manager_arr = ArrayHelper::map($manager_arr, 'id', 'realname');
            foreach ($employee as $val) {
                $re = ArrayHelper::merge($val, $val['employee']);
                array_push($employee_arr, $re);
            }
            $employee_arr = ArrayHelper::map($employee_arr, 'id', 'realname');

            return $this->render('create', ['model' => $model, 'de_arr' => $de_arr, 'manager' => $manager_arr, 'employee' => $employee_arr]);
        }

        return $this->render('create', ['model' => $model, 'de_arr' => $de_arr]);
    }

    /*
    * 部门列表
     */
    public function actionDatalist()
    {
        if(!Yii::$app->user->can('updateDepartment')){
            throw new ForbiddenHttpException('对不起，您现在还没获得此操作的权限');
        }

        return $this->render('datalist');
    }

    /*
    * 异步获取部门信息
    */
    public function actionDeparts()
    {
        $model = new DepartmentForm;
        $departments = Department::find()->select(['id', 'pid', 'name'])->asArray()->all();
        $dep_json = $model->returnArray($departments);
        
        return $dep_json;
    }


}    